﻿using System;
using System.Configuration;
using System.Web;
using Elmah;
using Uber.Core.Model;

namespace Uber.WebUI.Controllers
{
    public static class UiExtensions
    {
        public static void Raize(this Exception ex)
        {
            if(HttpContext.Current == null)
            ErrorLog.GetDefault(null).Log(new Error(ex));
            else
            ErrorSignal.FromCurrentContext().Raise(ex);
        }
    }

    public class Gl
    {
        public static string UserRole()
        {
            if (!HttpContext.Current.User.Identity.IsAuthenticated) return string.Empty;
            if (HttpContext.Current.User.IsInRole(The.Pro)) return The.Pro;
            if (HttpContext.Current.User.IsInRole(The.Nub)) return The.Nub;
            if (HttpContext.Current.User.IsInRole(The.Seeker)) return The.Seeker;
            return string.Empty;
        }

        public static long? Ref()
        {
            return (long?) HttpContext.Current.Session[The.Ref];
        }

        public static long UserId()
        {
            return !HttpContext.Current.Request.IsAuthenticated ? 0 : Convert.ToInt64(HttpContext.Current.User.Identity.Name);
        }

        public static string MediaUrl()
        {
            return ConfigurationManager.AppSettings["mediaUrl"];
        }

        public static bool Allow(long ownerId)
        {
            return (HttpContext.Current.User.IsInRole("admin") || ownerId == UserId());
        }
    }
}